package com.imooc.dianping.controller;

import com.imooc.dianping.common.CommonRes;
import com.imooc.dianping.enums.EmBusinessError;
import com.imooc.dianping.exception.BusinessException;
import com.imooc.dianping.model.CategoryModel;
import com.imooc.dianping.model.ShopModel;
import com.imooc.dianping.service.CategoryService;
import com.imooc.dianping.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("/shop")
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;
    @Autowired
    private CategoryService categoryService;


    //推荐服务v1.0
    @RequestMapping("/recommend")
    @ResponseBody
    public CommonRes recommend(@RequestParam(name = "latitude") BigDecimal latitude,
                               @RequestParam(name = "longitude") BigDecimal longitude) throws BusinessException {
        if (latitude == null || longitude == null) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        List<ShopModel> shopModelList = shopService.recommend(latitude, longitude);
        return CommonRes.create(shopModelList);
    }

    //搜索服务v1.0
    @RequestMapping("/search")
    @ResponseBody
    public CommonRes search(@RequestParam(name = "latitude") BigDecimal latitude,
                            @RequestParam(name = "longitude") BigDecimal longitude,
                            @RequestParam(name = "keyword") String keyword,
                            @RequestParam(name = "orderby", required = false) Integer orderby,
                            @RequestParam(name = "categoryId", required = false) Integer categoryId,
                            @RequestParam(name = "tags", required = false) String tags
    ) throws BusinessException, IOException {
        if (StringUtils.isEmpty(keyword) || latitude == null || longitude == null) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        // 搜索v2.0 模型
        Map<String, Object> result = shopService.searchES(latitude, longitude, keyword, orderby, categoryId, tags);
        List<ShopModel> shopModelList = (List<ShopModel>) result.get("shop");
        //使用数据库聚合shop门店信息，搜索都交给es处理
        List<CategoryModel> categoryModelList = categoryService.selectAll();
        // 搜索v1.0 模型 废弃
        // List<Map<String, Object>> tagsAggregation = shopService.searchGroupByTags(keyword, categoryId, tags);
        // 搜索v2.0 模型
        List<Map<String, Object>> tagsAggregation = (List<Map<String, Object>>) result.get("tags");
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("shop", shopModelList);
        resultMap.put("category", categoryModelList);
        resultMap.put("tags", tagsAggregation);
        return CommonRes.create(resultMap);
    }

}
